<template>
	<view class="content">
		<view class="solids-bottom padding-xs flex align-center">
			<view class="flex-sub text-center">
				<view class="solid-bottom text-sl padding"><text class=" cuIcon-roundcheckfill text-blue"></text></view>
				<view class="padding">我们需要您的授权获取手机，手机验证通过后平台将自动关联账单地址，识别业主身份</view>
			</view>
		</view>
		<view class="box margin-top-20" style=" padding: 20upx;">
			<button @getphonenumber="bindgetphonenumber"  open-type="getPhoneNumber" :loading="loading" 
          :disabled="disabled"  class="cu-btn block bg-blue margin-tb-sm lg">
				<text :class="loadding2 == 1 ? 'cuIcon-loading2 cuIconfont-spin' : 'cuIconfont-spin'"></text>
				获取手机号
			</button>
		</view>
	</view>
</template>

<script>
import beeApp from '../../beeApp.js';
export default {
	data() {
		return {
			islogin:0,
			title: 'Hello',
			getCodeText: '获取验证码',
			sendingcode: 0,
			loadding: 0,
			loadding2:0,
			disabled:0,
			pageType: 'bindphone',
			canIUse: wx ? wx.canIUse('button.open-type.getUserInfo') : 0
		};
	},
	onLoad(option) {
		if (option.page) {
			this.pageType = option.page;
		}
		 
		if (this.pageType == 'auth') {
			uni.setNavigationBarTitle({
				title: '授权登陆'
			});
		}
		// beeApp.alert("yiqwyei");
	},
	methods: {
		 login: function (postData){
		    var that = this;
			this.loadding = 1;
			var user_info = uni.getStorageSync('user_info');
			postData.userInfo = user_info;
			if(user_info.user_id){
				this.islogin=1;
			}
		    beeApp.loginByUserInfo(postData, function (res) {
		      console.log("loginByUserInfo", res);
			  that.loadding = 0;
		      if (res.code != 200) {
		        beeApp.alert(res.msg);
				return;
		      }
			  beeApp.alert("手机号绑定成功",()=>{
				  uni.removeStorageSync('user_info');
				  uni.setStorageSync('user_info', res.data);
				  beeApp.setPageData('pages/user/index', { reload: 1 });
				  uni.navigateBack();
			  })
			  
		    });
		  },
		bindgetphonenumber:function(e){
		    if (e.detail.errMsg!="getPhoneNumber:ok"){
		      beeApp.alert(e.detail.errMsg);
			  return
		    }else{
		      var postData = {};
		      postData.bindphone = 1;
		      postData.encryptedData = e.detail.encryptedData;
		      postData.iv = e.detail.iv;
		      console.log("postData",postData);
		      this.login(postData);
		    }
		
		  },
		
	}
};
</script>
